<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>受控组件 - 高阶函数</title>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class App extends React.Component{
            //1. 声明状态
            state = {
                user: '',
                pass: '',
                code: '',
                phone: ''
            }

            render(){
                return <div>
                        <h2>登录</h2>
                        <form action="">
                            {/**2. 为表单元素 设置 value 与 onChange **/}
                            用户名: <input type="text"  value={this.state.user}  onChange={this.saveData('user')}  /><br />    
                            密码: <input type="password" value={this.state.pass} onChange={this.saveData('pass')}  /><br />    
                            验证码: <input type="text" value={this.state.code} onChange={this.saveData('code')}  /><br />    
                            手机号: <input type="text" value={this.state.phone} onChange={this.saveData('phone')}  /><br />    
                            <input type="submit" value="登录" onClick={this.login} />
                        </form>
                    </div>
            }

            //方法
            saveData = (type) => {
                return (e) => {
                    this.setState({
                        [type]: e.target.value
                    });
                }
            }

            login = (e) => {
                //获取表单项的值
                e.preventDefault();

                console.log(this.state.user);
                console.log(this.state.pass);
            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"));
    </script>
</body>
</html>